package com.dh.project.controller;

import com.dh.project.model.MessageUtil;
import com.dh.project.other.BaseController;
import com.dh.project.utils.JsonUtil;
import com.dh.project.utils.LogUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by lex on 2017/1/24.
 */
@Controller
public class DemoController extends BaseController {

    @ResponseBody
    @RequestMapping(value = "wx")
    // post方法用于接收微信服务端消息
    public String wx(HttpServletRequest request, HttpServletResponse response) {
        LogUtil.i("=============================收到一个" + request.getMethod() + "请求=============================");
        switch (request.getMethod()) {
            default:
            case "GET":
                LogUtil.i("请求方式: GET");
                String token = "yitongwxtoken2016";
                String timestamp = request.getParameter("timestamp");
                String nonce = request.getParameter("nonce");
                String signature = request.getParameter("signature");
                String echostr = request.getParameter("echostr");

                LogUtil.i("微信请求参数: "+JsonUtil.toJson(request.getParameterMap()));

                return echostr.replace("\"","");
            case "POST":
                LogUtil.i("请求方式: POST");
                System.out.println("原始请求内容=============================" + request.getParameter("timestamp").toString());
                try {
                    Map<String, String> map = MessageUtil.parseXml(request);
                    System.out.println("转义后=============================" + JsonUtil.toJson(map));
                } catch (Exception e) {
                    LogUtil.e(e.getMessage());
                }
                return "200";
        }

    }


    @RequestMapping(value = "share")
    public String share(){
        return "share";
    }
}
